package com.piesat.quake.business.device.mqtt.reader;

import com.piesat.quake.business.device.mqtt.param.ParseParam;

import io.netty.buffer.ByteBuf;

/**
 * 字符读取器
 *
 * @author Gersy
 * @date 2023/2/6
 * @since 1.0
 */
public class CharReader implements Reader {

    @Override
    public String read(ByteBuf byteBuf, ParseParam param) {
        byte[] tmpBytes = new byte[param.getLength()];
        byteBuf.readBytes(tmpBytes);
        byte temp = 0;
        int totalLength = tmpBytes.length;
        int length = tmpBytes[totalLength - 1] == temp ? 0 : totalLength;
        if (length == 0) {
            for (int i = 0; i < totalLength; i++) {
                if (tmpBytes[i] == temp) {
                    length = i;
                    break;
                }
            }
        }
        return new String(tmpBytes, 0, length);
    }
}
